package kt.com.bilin.coin.ui.fragment.mill

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bilin.coin.ApiConstant
import com.bilin.coin.R
import com.bilin.coin.bean.CommonSuccessEvent
import com.bilin.coin.databinding.ItemMyMillBinding
import kotlinx.android.synthetic.main.activity_base_refresh.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.MyMillBean
import kt.com.bilin.coin.common.appendOSSURL
import kt.com.bilin.coin.common.formatDouble
import kt.com.bilin.coin.common.gone
import kt.com.bilin.coin.common.put
import kt.com.bilin.coin.helper.InjectBundle
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.ui.fragment.BaseRefreshFragment
import kt.com.bilin.coin.utils.GlideUtils
import kt.com.bilin.coin.viewmodel.BackgroundCommonApi.launchUI
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MyMillFragment : BaseRefreshFragment<MyMillBean>() {
    companion object {
        private const val TYPE = "mType"
        fun createInstance(type: Int): MyMillFragment = MyMillFragment().put(TYPE to type)
    }

    private val mType: Int by InjectBundle()
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        mBaseRecyclerAdapter = MyMillAdapter()
        super.initView(rootView, savedInstanceState)
        titleView.gone()
        refreshLayout.setEnableLoadMore(false)
    }

    override fun onReload() {
        launchUI({ API_SERVICE.myDevice(mRefreshHelper.page, mType).toData()?.records }, mRefreshHelper.handApiObserver())
    }

    /**
     * 登录成功
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun loginSuccessEvent(commonSuccessEvent: CommonSuccessEvent) {
        if (commonSuccessEvent.type == CommonSuccessEvent.LoginSuccess) {
            onReload()
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent)
    }

    /**
     * 认证成功
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun buyDevicesSuccessEvent(commonSuccessEvent: CommonSuccessEvent) {
        if (commonSuccessEvent.type == CommonSuccessEvent.BuyDevicesSuccess) {
            onReload()
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent)
    }

    inner class MyMillAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMyMillBinding>, MyMillBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemMyMillBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemMyMillBinding>, bean: MyMillBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    btConfirm.visibility = if (!canAgain) View.GONE else View.VISIBLE
                    GlideUtils.intoView(iv1.context, blDevice.imgUrl.appendOSSURL, iv1)
                    tvName.text = blDevice.name
                    tvGold.text = "金豆总奖励：${totalAward.formatDouble()}"
                    tvWorkTime.text = "工作时间：$workTimeStr"
                    tvValidTime.text = validTime
                    tvName.text = blDevice.name
                    setViewClick(position, btConfirm)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            val mMyMillBean = getItem(position)
            when (v.id) {
                R.id.bt_confirm -> launchUI({ API_SERVICE.again(mMyMillBean.id).toData() }, ApiObserver(this@MyMillFragment.requireActivity()) { onReload() })
            }
        }
    }
}